package com.grades.springboot.controller;

import com.grades.springboot.entity.Result;
import com.grades.springboot.entity.Teacher;
import com.grades.springboot.service.impl.AdminServiceImpl;
import com.grades.springboot.service.impl.PermissionVerificationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private PermissionVerificationServiceImpl pv;
    @Autowired
    private AdminServiceImpl adminService;

    @GetMapping("/getTeacherList")
    public Result<?> getTeacherList(HttpServletRequest request, Integer pageNum, Integer pageSize){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.getTeacherList(pageNum,pageSize);
    }

    @GetMapping("/getTeacherByClass")
    public Result<?> getTeacherByClass(HttpServletRequest request,Integer classId, Integer pageNum, Integer pageSize){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.getTeacherByClass(classId,pageNum,pageSize);
    }

    @GetMapping("/getTeacherBySubject")
    public Result<?> getTeacherBySubject(HttpServletRequest request,String subjectName, Integer pageNum, Integer pageSize){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.getTeacherBySubject(subjectName,pageNum,pageSize);
    }

    @GetMapping("/getTeacherById")
    public Result<?> getTeacherById(HttpServletRequest request,Integer id, Integer pageNum, Integer pageSize){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.getTeacherById(id,pageNum,pageSize);
    }

    @GetMapping("/getTeacherByName")
    public Result<?> getTeacherByName(HttpServletRequest request,String name, Integer pageNum, Integer pageSize){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.getTeacherByName(name,pageNum,pageSize);
    }

    @PostMapping("/addTeacher")
    public Result<?> addTeacher(HttpServletRequest request, Teacher teacher){
        if(!pv.permissionVerification(request)) {
            return Result.error("权限错误");
        }
        return adminService.addTeacher(teacher);
    }
}
